﻿using Microsoft.Office.Interop.Excel;
using PanhuaWin.通用;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.仓库.报表
{
    public partial class Form库存余额报表 : UIPage
    {
        private string 工厂编号1;

        public Form库存余额报表()
        {
            InitializeComponent();
        }

        private void uiLabel5_Click(object sender, EventArgs e)
        {

        }

        private void Form库存余额报表_Load(object sender, EventArgs e)
        {
            this.uiDatePicker1.Value = DateTime.Today.AddMonths(-1);
            this.uiDatePicker2.Value = DateTime.Today;
            pub.ListWorkShop(pub.sys_user, this.uiComboBoxWorkShop);
            pub.ListInventoryClass(this.uiComboBox存货分类);
        }

        private void uiButton查询_Click(object sender, EventArgs e)
        {
            ListData();
        }



        private void ListData()
        {
            string sql;
            string 开始日期 =this.uiDatePicker1.Value.ToString("yyyy-MM-dd");
            string 结束日期 = this.uiDatePicker2.Value.AddDays(1).ToString("yyyy-MM-dd");
            if (this.uiComboBox存货分类.SelectedText=="热轧")
            {
                sql = " SELECT  [WorkShopCode] 工厂编号,[WarehouseCode] 仓库编号,[StorageLocation] 库位编号, [CreateDate] ,[Approver],[T_GOOD_UNIT] 计量单位 ,[VolumeCode] 卷号,";
                sql +=" [VolumeCodeNew] 新卷号,[InventoryCode] 存货编号 ,[Size] 规格型号 ,[Address] 产地,[Weight] 期初数量 ,0 入库数量,0 出库数量 ,[T_GOOD_NAME] 存货名称     FROM  [View_StockIn] ";
                sql += " where CreateDate< '" + 开始日期 + "'";
                sql += " and   WarehouseCode ='" + this.uiComboBox仓库编号1.SelectedValue + "'";
                sql += " and   StorageLocation ='" + this.uiComboBox库位编号1.SelectedValue + "'";
                sql += " and   WorkShopCode ='" + this.uiComboBoxWorkShop.SelectedValue + "'";
                if (this.uiTextBox存货编号1.Text !="")
                {
                    sql += " and   InventoryCode ='" + this.uiTextBox存货编号1.Text.Trim().Replace("--","") + "'";
                }
                if (this.uiTextBox存货名称1.Text != "")
                {
                    sql += " and   T_GOOD_NAME  like '%" + this.uiTextBox存货名称1.Text.Trim().Replace("--", "") + "%'";
                }
                if (this.uiTextBox规格型号1.Text != "")
                {
                    sql += " and   Size  like '%" + this.uiTextBox规格型号1.Text.Trim().Replace("--", "") + "%'";
                }
                if (this.uiTextBox产地1.Text != "")
                {
                    sql += " and   Address  like '%" + this.uiTextBox产地1.Text.Trim().Replace("--", "") + "%'";
                }
                sql += " union all ";
                sql = " SELECT  [WorkShopCode] 工厂编号,[WarehouseCode] 仓库编号,[StorageLocation] 库位编号, [CreateDate] ,[Approver],[T_GOOD_UNIT] 计量单位 ,[VolumeCode] 卷号,";
                sql += " [VolumeCodeNew] 新卷号,[InventoryCode] 存货编号 ,[Size] 规格型号 ,[Address] 产地,0期初数量 ,[Weight] 入库数量,0 出库数量 ,[T_GOOD_NAME] 存货名称     FROM  [View_StockIn] ";
                sql += " where CreateDate>= '" + 开始日期 + "' and CreateDate<'"+ 结束日期 + "'";
                sql += " and   WarehouseCode ='" + this.uiComboBox仓库编号1.SelectedValue + "'";
                sql += " and   StorageLocation ='" + this.uiComboBox库位编号1.SelectedValue + "'";
                sql += " and   WorkShopCode ='" + this.uiComboBoxWorkShop.SelectedValue + "'";
                if (this.uiTextBox存货编号1.Text != "")
                {
                    sql += " and   InventoryCode ='" + this.uiTextBox存货编号1.Text.Trim().Replace("--", "") + "'";
                }
                if (this.uiTextBox存货名称1.Text != "")
                {
                    sql += " and   T_GOOD_NAME  like '%" + this.uiTextBox存货名称1.Text.Trim().Replace("--", "") + "%'";
                }
                if (this.uiTextBox规格型号1.Text != "")
                {
                    sql += " and   Size  like '%" + this.uiTextBox规格型号1.Text.Trim().Replace("--", "") + "%'";
                }
                if (this.uiTextBox产地1.Text != "")
                {
                    sql += " and   Address  like '%" + this.uiTextBox产地1.Text.Trim().Replace("--", "") + "%'";
                }
                sql += " union all ";                                                      
                sql += @" SELECT  [WorkShopCode] 工厂编号 ,[WarehouseCode] 仓库编号 ,[StorageLocation] 库位编号,[CreateDate],[Approver] ,  [T_GOOD_UNIT]计量单位  ,
,[RowNumber],[InventoryCode],[Size],[Batch],[QuantityMain] ,[QuantityAssist],[T_GOOD_NAME]
,[T_GOOD_TYPE] ,[T_GOOD_TYPE_CODE],
                FROM [View_StockOut] ";
                sql += " where CreateDate>= '" + 开始日期 + "' and CreateDate<'" + 结束日期 + "'";
                sql += " and   WarehouseCode ='" + this.uiComboBox仓库编号1.SelectedValue + "'";
                sql += " and   StorageLocation ='" + this.uiComboBox库位编号1.SelectedValue + "'";
                sql += " and   WorkShopCode ='" + this.uiComboBoxWorkShop.SelectedValue + "'";
                if (this.uiTextBox存货编号1.Text != "")
                {
                    sql += " and   InventoryCode ='" + this.uiTextBox存货编号1.Text.Trim().Replace("--", "") + "'";
                }
                if (this.uiTextBox存货名称1.Text != "")
                {
                    sql += " and   T_GOOD_NAME  like '%" + this.uiTextBox存货名称1.Text.Trim().Replace("--", "") + "%'";
                }
                if (this.uiTextBox规格型号1.Text != "")
                {
                    sql += " and   Size  like '%" + this.uiTextBox规格型号1.Text.Trim().Replace("--", "") + "%'";
                }
                if (this.uiTextBox产地1.Text != "")
                {
                    sql += " and   Address  like '%" + this.uiTextBox产地1.Text.Trim().Replace("--", "") + "%'";
                }


            }
            else
            {

            }

        }

        private void uiComboBoxWorkShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql;
            工厂编号1 = this.uiComboBoxWorkShop.SelectedValue.ToString();
            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号1 + "'      ";
            System.Data.DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                //                sql = @"SELECT    guid, WorkShopGuid, WarehouseCode, WarehouseName, WarehouseDesc, AuditDate, AuditStatus, AuditId, 
                //                Auditor, Remark, CreateID, Creator, CreateDate, ModifyID, Modifier, ModifyDate
                //FROM      Dic_Warehouse where WorkShopGuid ='" + tb.Rows[0]["guid"].ToString() + "'";
                //                DataTable tb2 = pub.GetDataTable(pub.ConnectionString, sql);
                pub.ListWareHouse(this.uiComboBox仓库编号1, tb.Rows[0]["guid"].ToString());

                if (this.uiComboBox仓库编号1.SelectedValue.ToString() != "")
                {
                    Do库位刷新1(this.uiComboBox仓库编号1.SelectedValue.ToString());
                }
                else
                {

                }

            }
        }

        private void Do库位刷新1(string 仓库编号)
        {
            string sql;

            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号1 + "'      ";
            System.Data.DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                this.uiComboBox库位编号1.DataSource = null;
                uiComboBox库位编号1.Refresh();
                pub.ListLocation(this.uiComboBox库位编号1, 工厂编号1, this.uiTextBox库位搜索1.Text, 仓库编号);
                pub.ListWareHouseALL(this.uiComboBox仓库编号1, 工厂编号1);
             
            }
            else
            {
                this.uiComboBox库位编号1.DataSource = null;
                uiComboBox库位编号1.Refresh();

            }
        }

        private void uiComboBox仓库编号1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Do库位刷新1(this.uiComboBox仓库编号1.SelectedValue.ToString());
        }

        private void uiTextBox库位搜索1_TextChanged(object sender, EventArgs e)
        {
            Do库位刷新1(this.uiComboBox仓库编号1.SelectedValue.ToString());
            
        }

        private void uiButton选择存货1_Click(object sender, EventArgs e)
        {
            Form存货档案 form存货档案 = new Form存货档案();
            form存货档案.存货分类 = "生产辅料";
            if (form存货档案.ShowDialog(this) == DialogResult.OK)
            {
                this.uiTextBox存货编号1.Text = form存货档案.存货档案.T_GOOD_CODE;
                this.uiTextBox存货名称1.Text = form存货档案.存货档案.T_GOOD_NAME;
                this.uiTextBox规格型号1.Text = form存货档案.存货档案.T_GOOD_MODEL;
               
            }
        }

        private void uiButton导出_Click(object sender, EventArgs e)
        {

        }
    }
}
